//
//  WEXEmoticonKeyBoardViewModel.swift
//  microBlog
//
//  Created by apple on 16/4/27.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

let WEXEmoticonKeyBoardMaxCol = 7
let WEXEmoticonKeyBoardMaxRow = 3

let WEXEmoticonKeyBoardEmoticonCount = WEXEmoticonKeyBoardMaxCol * WEXEmoticonKeyBoardMaxRow - 1

class WEXEmoticonKeyBoardViewModel: NSObject {
    
    
    // viewModel单例
    static let sharedViewModel: WEXEmoticonKeyBoardViewModel = WEXEmoticonKeyBoardViewModel()
    
    // 最近表情存储沙盒
    private lazy var path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, false).last! as NSString).stringByAppendingPathComponent("recentEmoticon.archive")

    // 定义表情的bundle
    lazy var emoticonBundle: NSBundle = {
        
       let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
        
    }()
    
    // 定义最近表情的数组
    lazy var recentEmoticon: [WEXEmoticonModel] = {
        
        if let result = NSKeyedUnarchiver.unarchiveObjectWithFile(self.path) as? [WEXEmoticonModel]{
            
            return result
            
        }else{
            
            return [WEXEmoticonModel]()
        }
        
    }()
    
    // 定义默认表情的数组
    
    lazy var defaultEmoticon: [WEXEmoticonModel] = {
        
        return self.emoticonsArray("default")
        
    }()

    // emoji表情数组
    lazy var emojiEmoticon: [WEXEmoticonModel] = {
        
        return self.emoticonsArray("emoji")
    }()
    
    // lxh表情数组
    lazy var lxhEmoticon: [WEXEmoticonModel] = {
        
        return self.emoticonsArray("lxh")
    
    }()
    
    // 所有需要的数组
    lazy var allEmoticonArray:[[[WEXEmoticonModel]]] = {
       
        return [
        
            [self.recentEmoticon],
            self.emticonsPageArray(self.defaultEmoticon),
            self.emticonsPageArray(self.emojiEmoticon),
            self.emticonsPageArray(self.lxhEmoticon)
        ]
        
    }()
    
//    取出与字符串对应的表情模型
    func emotionWithChs(chs: String) -> WEXEmoticonModel? {
        
//        从默认表情数组中查询对应表情
        for value in defaultEmoticon{
            
            if value.chs == chs{
                
                return value
            }
        }
        
        for value in lxhEmoticon{
            
            if value.chs == chs{
                
                return value
            }
        }
        
        return nil
    }
    
    
    // MARK: - 保存最近使用的表情
    func saveRecentEmoticon(emoticon: WEXEmoticonModel){
        
//        取到第零组第零个表情
        var recentEmoticons = allEmoticonArray[0][0]
        
//        取到重复表情的数组
        let repeatEmoticon = recentEmoticons.filter { (value) -> Bool in
            
            if value.type == emoticon.type {
                
                if value.type == "1"{
                    
                    return value.code == emoticon.code
                    
                }else{
                    
                    return value.chs == emoticon.chs
                }
            }else{
                
                return false
            
            }
    
    }
//        移除重复表情
        for value in repeatEmoticon {
            
            if let index = recentEmoticons.indexOf(value){
                
                recentEmoticons.removeAtIndex(index)
            }
        }
        
        
//        将选中的表情添加到最近表情数组中
        recentEmoticons.insert(emoticon, atIndex: 0)
//        当最近的表情大于20个的时候，移除最后一个表情
        while recentEmoticons.count > 20 {
            
            recentEmoticons.removeLast()
        }
//        重新将最近表情赋值
        allEmoticonArray[0][0] = recentEmoticons
        
        NSKeyedArchiver.archiveRootObject(allEmoticonArray[0][0], toFile: path)
        
}
    
        
        
    
    
    // MARK: - 表情数组抽取方法
    private func emoticonsArray(filePath: String) -> [WEXEmoticonModel]{
        
        //        获取文件路径
        let path = self.emoticonBundle.pathForResource("\(filePath)/info.plist", ofType: nil)!
        
        //        获取数组
        let array = NSArray(contentsOfFile: path)!
        
        var tempArray =  [WEXEmoticonModel]()
        
        //        遍历数组
        for value in array {
            
            let model = WEXEmoticonModel(dict: (value as! [String : AnyObject]))
            

            
            

            
            model.folderName = filePath
            
            tempArray.append(model)
            
        }
        return tempArray
        
        
    }
    
    // MARK: - 获取数组分页
    private func emticonsPageArray(emoticons: [WEXEmoticonModel]) -> [[WEXEmoticonModel]]{
        
        // 返回页数
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        var tempArray: [[WEXEmoticonModel]] = [[WEXEmoticonModel]]()
        
        
        for i in 0..<pageCount{
            
            let loc = 20 * i
            
            var len = 20
            
            if loc + len > emoticons.count {
                
                len = emoticons.count - loc
            }

            let childArray = ((emoticons as NSArray).subarrayWithRange(NSMakeRange(loc, len))) as! [WEXEmoticonModel]
            
            tempArray.append(childArray)
            
        }
        
        return tempArray
        
        
    }
    
}
